"use strict";
/* Controllers */
app.controller("wordsChartController", ['$scope', '$location', '$rootScope', '$routeParams', 'wordChartService', function ($scope, $location, $rootScope, $routeParams, wordChartService) {

    var vm = $scope.vm = {};
    vm.p = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.searchWordChart = function () {
        $("#refreshTable").addClass('disabled');
        var vm = $scope.vm;
        var params = {};
        params = vm.p

        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.areaNames = data.areaNames
            initChart(data)
            $("#refreshTable").removeClass('disabled');
        };

        wordChartService.getWordChart(params, procFn, errorFn);
    }

    $scope.clearSearch = function () {
        var vm = $scope.vm;
        vm.p.searchType = ''
        vm.p.endpoint = ''
        vm.p.areaName = ''
    }


    function initChart(resultData) {
        var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        window.onresize = function () {
            myChart.resize();
        };
        var app = {};

        var option;

        option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            xAxis: {
                type: 'category',
                data: resultData.xAxisData
            },
            yAxis: [
                {
                    name: '搜索量',
                    type: 'value'
                }
            ],
            series: [
                {
                    name: $scope.vm.p.inword,
                    yAxisIndex: 0,
                    data: resultData.values,
                    type: 'line',
                    smooth: true
                }
            ]
        };

        window.onresize = function () {
            myChart.resize();
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    if ($routeParams.param != null) {
        vm.p = JSON.parse(decodeURIComponent($routeParams.param));
        if (vm.p.catename != '' && typeof (vm.p.catename) != "undefined" && vm.p.catename.indexOf('|') >= 0) {
            vm.p.catename = vm.p.catename.replaceAll('|', '/')
        }
        $scope.searchWordChart()
    }

}]);


/* Services */
app.service('wordChartService', ['ajaxService', function (ajaxService) {

    this.getWordChart = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/wordTop/getWordChart.do", successFunction, errorFunction);
    };

}]);

